/**
 * Created by hjx on 2019/3/4.
 */
(function () {
    var exports = this;
    var $1;
    var $2;
    var $3;
    var $4;
    var ShareLayer_znq = cc.Layer.extend({
        ctor: function () {
            this._super();
            var that = this;
            loadNodeCCS(res.znq_ShareLayer, this);
            $1 = create$(this.getChildByName("Layer"));
            TouchUtil.setOnclickListener($1('root'), function () {
                that.removeFromParent();
            });
            TouchUtil.setOnclickListener($1('btn_close'), function () {
                that.removeFromParent();
            });
            TouchUtil.setOnclickListener($1('btn_group'), function () {
                that.group = true;
                that.share(that.group);
            });
            TouchUtil.setOnclickListener($1('btn_quan'), function () {
                that.group = false;
                that.share(that.group);
            });
            return true;
        },

        share: function () {
            var scene = loadNodeCCS(activity.res.znq_ShareNodeJson);
            if (hall.res.erweima) {
                var erweima = scene.getChildByName('erweima')
                if (erweima) {
                    erweima.setTexture(hall.res.erweima);
                }
            }
            if (this.group) {
                ShareUtil.SharePic(scene, ShareUtil.SharePlatForm.WeiXin);
            } else {
                ShareUtil.SharePic(scene, ShareUtil.SharePlatForm.WeiXinQuan);
            }
            this.removeFromParent();
        }
    });

    var RuleLayer_znq = cc.Layer.extend({
        ctor: function () {
            this._super();
            var that = this;
            loadNodeCCS(res.znq_RuleLayer, this);
            $2 = create$(this.getChildByName("Layer"));
            TouchUtil.setOnclickListener($2('root'), function () {
                that.removeFromParent();
            });
            TouchUtil.setOnclickListener($2('btn_ok_rule'), function () {
                that.removeFromParent();
            });
            TouchUtil.setOnclickListener($2('btn_close'), function () {
                that.removeFromParent();
            });
            return true;
        }
    });

    var ReceiveLayer_znq = cc.Layer.extend({
        ctor: function () {
            this._super();
            var that = this;
            loadNodeCCS(res.znq_ShareRedLayer, this);
            that._index = 0;
            this.getToken();
            $3 = create$(this.getChildByName("Layer"));
            TouchUtil.setOnclickListener($3('root'), function () {
                that.removeFromParent();
            });
            TouchUtil.setOnclickListener($3('btn_close'), function () {
                that.removeFromParent();
            });
            var share = function () {
                var func = function () {
                    var Url = "";
                    if (gameData.opt_conf_2["ZNQHD"] == 1) {
                        Url = "http://pay.yayayouxi.com/mochuang/red_packet/index.html";
                    } else if (gameData.opt_conf_2["ZNQHD"] == 2) {
                        Url = "http://pay.yayayouxi.com/mochuang-test1/red_packet/index.html";
                    }

                    Url += '?area=' + app.area;
                    Url += '&userId=' + gameData.uid;
                    Url += '&token=' + that._token;
                    Url += '&role=4';
                    Url += '&amount=' + that._amount;
                    Url += '&link=' + app.shareDownLoadUrl;
                    Url += '&appid=' + app.appId;
                    if (that.group) {
                        WeChatUtil.shareUrl(Url, "丫丫游戏公司过生日，海量现金红包免费领~你还在等什么？~", "这个公司真大方,玩游戏免费得现金红包,还人人都有,你~不来拿吗?", 0, getCurTimestamp() + gameData.uid)
                    } else {
                        WeChatUtil.shareUrl(Url, "丫丫游戏周年庆", "丫丫游戏公司过生日，海量现金红包免费领~你还在等什么？~", 1, getCurTimestamp() + gameData.uid)
                    }
                    that.removeFromParent()
                }
                if (that._token && that._amount) {
                    func();
                } else {
                    that.getToken(func)
                }
            }
            TouchUtil.setOnclickListener($3('btn_group'), function () {
                that.group = true;
                share();
            });
            TouchUtil.setOnclickListener($3('btn_quan'), function () {
                that.group = false;
                share();
            });
            return true;
        },
        getToken: function (cb) {
            var that = this;
            this._token = undefined;
            this._amount = undefined;
            var tokenUrl = '';
            if (gameData.opt_conf_2["ZNQHD"] == 1) {
                tokenUrl = 'https://pay.yayayouxi.com/fyactivity/app/rlist';
            } else if (gameData.opt_conf_2["ZNQHD"] == 2) {
                tokenUrl = "https://pay.yayayouxi.com/fyactivity-test/app/rlist"
            }
            showLoading("正在加载红包信息...");
            var data = {
                'playerId': gameData.uid,
                'area': app.area,
                'activityType': 55,
                'signKey': Crypto.MD5("feiyu-activity" + gameData.uid + app.area)
            };
            http.post(tokenUrl, data,
                function (data) {
                    cc.log(JSON.stringify(data));
                    hideLoading();
                    if (data.success && data.code == 0 && data.data) {
                        that._token = data.data.token;
                        that._amount = data.data.num;
                        if (data.data.num == 0) {
                            showAlert(data.message)
                        } else {
                            cb();
                        }
                    } else {
                        showAlert(data.message)
                    }
                },
                function () {
                    hideLoading();
                    that.removeFromParent();
                    showAlert('请求红包信息失败，请稍后再试.')
                }
            )
        }
    });
    var ReceiveRuleLayer_znq = cc.Layer.extend({
        ctor: function () {
            this._super();
            var that = this;
            loadNodeCCS(res.znq_ReceiveRuleLayer, this);
            $4 = create$(this.getChildByName("Layer"));
            TouchUtil.setOnclickListener($4('btn_close'), function () {
                that.removeFromParent();
            });
            $4('bg2').setVisible(false);
            TouchUtil.setOnclickListener($4('bg1.btn_next'), function () {
                $4('bg2').setVisible(true);
                $4('bg1').setVisible(false);
            });
            TouchUtil.setOnclickListener($4('bg2.btn_last'), function () {
                $4('bg1').setVisible(true);
                $4('bg2').setVisible(false);
            });
            TouchUtil.setOnclickListener($4('bg2.btn_goto'), function () {
                cc.log("领取红包");
                that.getParent().addChild(new ReceiveLayer_znq());
                that.removeFromParent();
            });
        }
    });

    exports.ShareLayer_znq = ShareLayer_znq;
    exports.RuleLayer_znq = RuleLayer_znq;
    exports.ReceiveLayer_znq = ReceiveLayer_znq;
    exports.ReceiveRuleLayer_znq = ReceiveRuleLayer_znq;
})(window);